package com.bjpowernode.app.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;

public class MyRealm extends AuthorizingRealm {
    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        System.out.println("授权");
        String tempUsername = principals.getPrimaryPrincipal().toString();
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        if (tempUsername.equals("admin1")) {
            simpleAuthorizationInfo.addStringPermission("admin:index");
        }
        if (tempUsername.equals("admin2")) {
            simpleAuthorizationInfo.addStringPermission("admin:index");
            simpleAuthorizationInfo.addStringPermission("admin:index2");
        }
        return simpleAuthorizationInfo;
    }
    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        String name = "admin";
        String password = "123456";
        String tempUsername = token.getPrincipal().toString();
        if (tempUsername.equals("admin1") || tempUsername.equals("admin2")) {
            SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(tempUsername, password, getName());
            return simpleAuthenticationInfo;
        } else {
            return null;
        }
    }
}